﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Yu.Cms.Common;
using Yu.Cms.IDAL;

namespace Yu.Cms.SQLServerDAL
{
    public class albumType : IalbumType
    {

        private readonly string TableName = "Y_albumType";

        #region IalbumType 成员

        /// <summary>
        /// 新增 lbumType (Y_albumType)
        /// </summary>
        /// <param name="model">Y_albumType实体类</param>
        /// <returns>-1:执行失败,Insert:</returns>
        public int Add(Yu.Cms.Model.albumType model)
        {
            int iSort = DbHelperMsSQL.GetMaxID(TableName, "alSort", null);
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " + TableName + "(");
            strSql.Append("alBigName,alBigInfo,pubTime,alSort,alTopID)");
            strSql.Append(" values (");
            strSql.Append("@alBigName,@alBigInfo,@pubTime," + iSort + ",@alTopID)"); 
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@alBigName", SqlDbType.NVarChar,15),
					new SqlParameter("@alBigInfo", SqlDbType.NVarChar,150),
					new SqlParameter("@pubTime", SqlDbType.SmallDateTime),
                    new SqlParameter("@alTopID",SqlDbType.Int,4)
					};
            parameters[0].Value = model.alBigName;
            parameters[1].Value = model.alBigInfo;
            parameters[2].Value = model.pubTime;
            parameters[3].Value = model.alTopID; 

            object obj = DbHelperMsSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return -1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 修改 lbumType (Y_albumType)
        /// </summary>
        /// <param name="model">Y_albumType实体类</param>
        /// <returns>-1:执行失败,Update:返回更新记录数</returns>
        public int Update(Yu.Cms.Model.albumType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update "+TableName+" set ");
            strSql.Append("alBigName=@alBigName,");
            strSql.Append("alBigInfo=@alBigInfo,");
            strSql.Append("pubTime=@pubTime,");
            strSql.Append("alTopID=@alTopID");
            strSql.Append(" where alBigID=@alBigID ");
            SqlParameter[] parameters = {
					new SqlParameter("@alBigID", SqlDbType.Int,4),
					new SqlParameter("@alBigName", SqlDbType.NVarChar,15),
					new SqlParameter("@alBigInfo", SqlDbType.NVarChar,150),
					new SqlParameter("@pubTime", SqlDbType.SmallDateTime),
                    new SqlParameter("@alTopID",SqlDbType.Int,4)
					};
            parameters[0].Value = model.alBigID;
            parameters[1].Value = model.alBigName;
            parameters[2].Value = model.alBigInfo;
            parameters[3].Value = model.pubTime;
            parameters[4].Value = model.alTopID; 
            return DbHelperMsSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除 lbumType (Y_albumType)
        /// </summary>
        /// <param name="model">Y_albumType实体类</param>
        /// <returns>-1:执行失败,Delete:返回删除记录数</returns>
        public int Delete(int alBigID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from  "+TableName);
            strSql.Append(" where alBigID=@alBigID ");
            SqlParameter[] parameters = {
					new SqlParameter("@alBigID", SqlDbType.Int,4)};
            parameters[0].Value = alBigID;
            return DbHelperMsSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 是否存在封面相册
        /// </summary>
        /// <param name="alBigID"></param>
        /// <param name="alID"></param>
        /// <returns></returns>
        public bool isExistSmalID(int alBigID, int alID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from  " + TableName);
            strSql.Append(" where alBigID=@alBigID and alTopID=@alTopID ");
            SqlParameter[] parameters = {
                    new SqlParameter("@alBigID", SqlDbType.Int,4),
					new SqlParameter("@alTopID", SqlDbType.Int,4)};
            parameters[0].Value = alBigID;
            parameters[1].Value = alID;
            return DbHelperMsSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加点击次数+1
        /// </summary>
        /// <param name="alBigID"></param>
        public void UpdateAlbumClick(int alBigID)
        {
            DbHelperMsSQL.Update_Table_Fileds(TableName, " alClick=alClick+1 ", " alBigID=" + alBigID);
        }

        /// <summary>
        /// 评论次数+1
        /// 或评论次数-1
        /// </summary>
        /// <param name="alBigID"></param>
        public void UpdateAlumComment(int alBigID,Yu.Enum.AddSub AddSub)
        {
            if (AddSub == Yu.Enum.AddSub.Add)
            {
                DbHelperMsSQL.Update_Table_Fileds(TableName, " alComment=alComment+1 ", " alBigID=" + alBigID);
            }
            else if (AddSub == Yu.Enum.AddSub.Sub)
            {
                DbHelperMsSQL.Update_Table_Fileds(TableName, " alComment=alComment-1 ", " alBigID=" + alBigID);
            }
        }


        /// <summary>
        /// 根据alBigID返回 albumTyp实体类 单笔资料 (Y_albumType)
        /// </summary>
        /// <param name="alBigID">alBigID 自动ID</param>
        /// <returns>返回 albumType实体类 alBigID为0则无记录</returns>
        public Yu.Cms.Model.albumType GetModel(int alBigID)
        {
            QueryParam qp = new QueryParam();
            qp.Where = " Where alBigID=" + alBigID;
            Yu.Cms.Model.albumType fam = new Yu.Cms.Model.albumType();
            int RecordCount;
            IList<Yu.Cms.Model.albumType> lst = this.GetList(qp, out RecordCount);
            if (lst.Count > 0)
            {
                fam = lst[0];
            }
            return fam;
        }

        /// <summary>
        /// 返回albumType实体类的IList对象 (Y_albumType)
        /// </summary>
        /// <param name="isEmpty">是否显示空相册 True:显示 False：不显示</param>
        /// <param name="CurrentPageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="RecordCount"></param>
        /// <returns></returns>
        public IList<Yu.Cms.Model.albumType> GetList(bool isEmpty, int CurrentPageIndex, int PageSize, out int RecordCount)
        {
            QueryParam qp = new QueryParam();
            qp.PageIndex = CurrentPageIndex;
            qp.PageSize = PageSize;
            if (!isEmpty)
            {
                qp.Where = " Where alphotoCount!=0";
            }
            return this.GetList(qp, out RecordCount);
        }

        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="alSort"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        public bool SortAlbumType(Yu.Cms.Model.albumType model, Yu.Enum.Sort sort)
        {
            int alSort = model.alSort;
            int alBigID = model.alBigID;
            return DbHelperMsSQL.SwitchSort(TableName, "alSort", alSort, "alBigID", alBigID, null, 0, sort);
        }

        #endregion

        /// <summary>
        /// 将记录集转为albumType实体类 (Y_albumType)
        /// </summary>
        /// <param name="dr">记录集</param>
        /// <param name="Fileds">字段名列表</param>
        /// <returns>lbumType</returns>
        protected Yu.Cms.Model.albumType Populate_albumType(IDataReader dr, Dictionary<string, string> Fileds)
        {
            Yu.Cms.Model.albumType nc = new Yu.Cms.Model.albumType();
            if (Fileds.ContainsKey("alBigID") && !Convert.IsDBNull(dr["alBigID"])) nc.alBigID = Convert.ToInt32(dr["alBigID"]); // 自动ID
            if (Fileds.ContainsKey("alBigName") && !Convert.IsDBNull(dr["alBigName"])) nc.alBigName = Convert.ToString(dr["alBigName"]).Trim(); // 相册名称
            if (Fileds.ContainsKey("alBigInfo") && !Convert.IsDBNull(dr["alBigInfo"])) nc.alBigInfo = Convert.ToString(dr["alBigInfo"]).Trim(); // 相册描述
            if (Fileds.ContainsKey("pubTime") && !Convert.IsDBNull(dr["pubTime"])) nc.pubTime = Convert.ToDateTime(dr["pubTime"]); // 创建时间
            if (Fileds.ContainsKey("alSort") && !Convert.IsDBNull(dr["alSort"])) nc.alSort = Convert.ToInt32(dr["alSort"]); // 排序
            if (Fileds.ContainsKey("alTopID") && !Convert.IsDBNull(dr["alTopID"])) nc.alTopID = Convert.ToInt32(dr["alTopID"]); // 相册封面ID
            if (Fileds.ContainsKey("alClick") && !Convert.IsDBNull(dr["alClick"])) nc.alClick = Convert.ToInt32(dr["alClick"]); // 点击次数
            if (Fileds.ContainsKey("alTopID") && !Convert.IsDBNull(dr["alComment"])) nc.alComment = Convert.ToInt32(dr["alComment"]); // 评论次数
            return nc;
        }

        /// <summary>
        /// 返回Y_albumType实体类的IList对象 (Y_albumType)
        /// </summary>
        /// <param name="qp">查询类(非安全函数,传入参数请进行Sql字符串过滤)</param>
        /// <param name="RecordCount">返回记录总数</param>
        /// <returns>albumType实体类的IList对象(Y_albumType)</returns>
        protected IList<Yu.Cms.Model.albumType> GetList(QueryParam qp, out int RecordCount)
        {
            qp.TableName = TableName;
            qp.OrderType = 0;
            qp.Orderfld = "alSort";
            if (qp.ReturnFields == null)
            {
                qp.ReturnFields = "*";
            }
            else
            {
                qp.ReturnFields += ",";
                qp.ReturnFields += qp.Orderfld;
            }
            DbHelperMsSQL.PopulateDelegate<Yu.Cms.Model.albumType> mypd = new DbHelperMsSQL.PopulateDelegate<Yu.Cms.Model.albumType>(this.Populate_albumType);
            return DbHelperMsSQL.GetRecordListBySql(mypd, qp, out RecordCount);
        }
    }
}
